package marmalius;

import java.util.ArrayList;

import marmalius.player.Item;

/*
	Effectue la gestion couper du bois
	modifie par Metos [EOAH]
*/

public class Woodcutting {
	
	private ArrayList<TreeInfo> trees;
	
	public Woodcutting () {
		trees = new ArrayList<TreeInfo>();
	}
	
	public void process () {
		for (int i = 0; i < trees.size(); i++) {
			TreeInfo tmp = trees.get(i);
			if (tmp.life <= 0) {
				if (tmp.respawn > 0) {
					trees.get(i).respawn--;
				}
				else {
					//TODO mettre a jour l'affichage de l'arbre (present)
					trees.remove(i);
				}
			}
		}
	}
	
	public void ResetWoodCutting (client player) {
		player.TreeId = 0;
		player.WCTimer = 0;
		player.TreeX = 0;
		player.TreeY = 0;
		player.TreeTimer = 0;
		player.resetAnimation();
		player.resetanim = 4;
	}
	
	public void updateWoodCuttingTimer (client player) {
		if (player.WCTimer > 0) {
			player.WCTimer--;
		}
	}
	
	public void updateWoodCutting (client player) {
		if (player.WCTimer <= 0) {
			if (playerHaveAxe(player)) {
				TreeInfo tree = getTreeInfo(player.TreeId, player.TreeX, player.TreeY);
				player.addSkillXP(tree.xp, 8);
				if (tree.life > 0 && player.addItem(tree.log, 1)) {
					player.sendMessage("You get some logs.");
					tree.life--;
					if (tree.life > 0) {
						player.WCTimer = player.TreeTimer;
						player.ContinuedAnim(0x284);
					}
					else {
						//TODO mettre a jour l'affichage de l'arbre (coupe)
						ResetWoodCutting(player);
					}
				}
				else {
					ResetWoodCutting(player);
				}
			}
			else {
				player.sendMessage("You need an axe to chop down this tree.");
				ResetWoodCutting(player);
			}
		}
	}
	
	public boolean checkWoodCutting (int treeId, client player, int x, int y) {
		TreeInfo tree = getTreeInfo(treeId, x, y);
		if (tree.treeId == 0) {
			return false;
		}
		if (playerHaveAxe(player)) {
			if (player.WCTimer <= 0) {
				if (player.playerLevel[8] >= tree.lvlReq) {
					if (tree.life > 0) {
						player.ContinuedAnim(0x284);
						player.resetanim = 999;
						player.sendMessage(tree.logName);
						player.TreeX = x;
						player.TreeY = y;
						player.TreeTimer = GetWoodCuttingTreeTime(treeId, player.playerLevel[8]);
						player.WCTimer = player.TreeTimer;
						player.updateRequired = true;
						player.appearanceUpdateRequired = true;
						return true;
					}
					else {
						player.sendMessage("Il n'y a plus de bois sur cet arbre.");
					}
				}
				else  {
					player.sendMessage("You need a woodcutting level of " + tree.lvlReq + " to chop down this tree.");
				}
			}
		}
		else {
			player.sendMessage("You need an axe.");
			ResetWoodCutting(player);
		}
		return false;
	}
	
	private int GetWoodCuttingTreeTime (int treeId, int woodCuttingLevel) {
		int lvlB = woodCuttingLevel / 4;
		
		switch (treeId) {
			case 1276:
			case 1277:
			case 1278:
			case 1279:
			case 1280:
			case 1282:
			case 1283:
			case 1284:
			case 1285:
			case 1286:
			case 1289:
			case 1290:
			case 1291:
			case 1315:
			case 1316:
			case 1318:
			case 1319:
			case 1330:
			case 1331:
			case 1332:
			case 1365:
			case 1383:
			case 1384:
			case 2409:
			case 3033:
			case 3034:
			case 3035:
			case 3036:
			case 3881:
			case 3882:
			case 3883:
			case 5902:
			case 5903:
			case 5904:
				return 25 - lvlB;
			case 1281:
			case 3037:
				return 28 - lvlB;
			case 1308:
			case 5551:
			case 5552:
			case 5553:
				return 31 - lvlB;
			case 1307:
			case 4674:
				return 34 - lvlB;
			case 1309:
				return 37 - lvlB;
			case 1292:
			case 1306:
				return 40 - lvlB;
		}
		return 0;
	}
	
	private TreeInfo getTreeInfo (int treeId, int x, int y) {
		for (int i = 0; i < trees.size(); i++) {
			TreeInfo tmp = trees.get(i);
			if (tmp.treeId == treeId && tmp.x == x && tmp.y == y) {
				return trees.get(i);
			}
		}
		
		TreeInfo tree = new TreeInfo();
		switch (treeId) {
			case 1276: // Level 1, XP 25, Log 1511, Name Tree
			case 1277:
			case 1278: // Level 1, XP 25, Log 1511, Name Tree
			case 1279:
			case 1280:
			case 1282:
			case 1283:
			case 1284:
			case 1285:
			case 1286: // Level 1, XP 25, Log 1511, Name Dead Tree
			case 1289:
			case 1290:
			case 1291:
			case 1315:
			case 1316:
			case 1318:
			case 1319:
			case 1330:
			case 1331:
			case 1332:
			case 1365:
			case 1383:
			case 1384:
			case 2409:
			case 3033:
			case 3034:
			case 3035:
			case 3036:
			case 3881:
			case 3882:
			case 3883:
			case 5902:
			case 5903:
			case 5904:
				tree.treeId = treeId;
				tree.x = x;
				tree.y = y;
				tree.lvlReq = 1;
				tree.xp = 25;
				tree.log = 1511;
				tree.life = 1;
				tree.respawn = 5;
				tree.logName = "You begin to cut the tree.";
				break;
			case 1281: // Level 15, XP 37.5, Log ?, Name Oak
			case 3037:
				tree.treeId = treeId;
				tree.x = x;
				tree.y = y;
				tree.lvlReq = 15;
				tree.xp = 37;
				tree.log = 1521;
				tree.life = 2;
				tree.respawn = 7;
				tree.logName = "You begin to cut the oak tree.";
				break;
			case 1308:
			case 5551:
			case 5552:
			case 5553:
				tree.treeId = treeId;
				tree.x = x;
				tree.y = y;
				tree.lvlReq = 35;
				tree.xp = 50;
				tree.log = 1519;
				tree.life = 5;
				tree.respawn = 20;
				tree.logName = "You begin to cut the willow tree.";
				break;
			case 1307:
			case 4674:
				tree.treeId = treeId;
				tree.x = x;
				tree.y = y;
				tree.lvlReq = 45;
				tree.xp = 75;
				tree.log = 1517;
				tree.life = 8;
				tree.respawn = 25;
				tree.logName = "You begin to cut the maple tree.";
				break;
			case 1309:
				tree.treeId = treeId;
				tree.x = x;
				tree.y = y;
				tree.lvlReq = 45;
				tree.xp = 75;
				tree.log = 1515;
				tree.life = 10;
				tree.respawn = 30;
				tree.logName = "You begin to cut the yew tree.";
				break;
			case 1292:
			case 1306:
				tree.treeId = treeId;
				tree.x = x;
				tree.y = y;
				tree.lvlReq = 75;
				tree.xp = 100;
				tree.log = 1513;
				tree.life = 20;
				tree.respawn = 15;
				tree.logName = "You begin to cut the magic tree.";
				break;
			default:
				tree.treeId = 0;
				tree.x = 0;
				tree.y = 0;
				tree.lvlReq = 0;
				tree.xp = 0;
				tree.log = 0;
				tree.logName = "";
		}
		trees.add(tree);
		return tree;
	}
	
	private class TreeInfo {
		public int treeId;
		public int x;
		public int y;
		public int lvlReq;
		public int xp;
		public int log;
		public int life;
		public int respawn;
		public String logName;
	}
	
	public boolean playerHaveAxe (client player) {
		Item axe = player.getInventory().getAxe();
		return axe != null;
		/*
		int [] itemAxe = new int[8];
		itemAxe[0] = 6739;
		itemAxe[1] = 1351;
		itemAxe[2] = 1349;
		itemAxe[3] = 1353;
		itemAxe[4] = 1355;
		itemAxe[5] = 1357;
		itemAxe[6] = 1359;
		itemAxe[7] = 1361;
		for (int j = 0; j < itemAxe.length; j++) {
			for (int i = 0; i < player.playerItems.length; i++) {
				if (player.playerItems[i] == itemAxe[j] + 1) {
					player.playerAxe = itemAxe[j];
					return true;
				}
			}
			for (int i2 = 0; i2 < player.playerEquipment.length; i2++) {
				if (player.playerEquipment[i2] == itemAxe[j]) {
					player.playerAxe = itemAxe[j];
					return true;
				}
			}
		}
		return false;
		*/
	}
	
}
